﻿using ParkSaleForAdmin.Data;
using ParkSaleForAdmin.Foundation;
using System.Windows;
using System.Windows.Controls;

namespace ParkSaleForAdmin.FunctionWindow
{
    public class ParkInfo
    {
        [DataToUI("车位代号", false)]
        public string id
        {
            get;
            set;
        }
        [DataToUI("小区代号")]
        public string location
        {
            get;
            set;
        }
        [DataToUI("售价")]
        public string price
        {
            get;
            set;
        }
        [DataToUI("小区内车位号")]
        public string number
        {
            get;
            set;
        }
        [DataToUI("车位状态,-1:已售 0:已预售 1:在售")]
        public string status
        {
            get;
            set;
        }
        [DataToUI("备注")]
        public string note
        {
            get;
            set;
        }

        //public ParkInfo Parse(DatabaseData data)
        //{
        //    id = int.Parse(data["id"].Value);
        //    number = int.Parse(data["number"].Value);
        //    status = int.Parse(data["status"].Value);
        //    price = double.Parse(data["price"].Value);
        //    location = int.Parse(data["location"].Value);
        //    note = data["note"].Value;
        //    return this;
        //}
    }
    public class ParkWindow : BasicManagerWindow<ParkInfo>
    {
        public ParkWindow()
        {
            Title = "车位管理";

        }
        protected override void BeginToCreate()
        {
            base.BeginToCreate();
            var item = new MenuItem() { Header = "摇号" };
            item.Click += Item_Click;
            featurebox.Items.Add(item);
        }

        private void Item_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                ParkInfo info = (ParkInfo)listView.SelectedItem;
                if (info.status.Equals("1"))
                {
                    ParkSaleForAdmin.SystemManager.Request<int>(SuUserRequestInfo.s_Random, delegate (object obj)
                    {
                        if (obj != null)
                        {
                            var r = (int)obj;
                            if (r == SuUserResponseInfo<int>.OK)
                            {
                                MessageBox.Show("摇号成功");

                            }
                            else
                            {
                                MessageBox.Show($"摇号失败，Code:{r}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                        else
                        {
                            MessageBox.Show("摇号失败，网络错误", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }

                    }, null, info.id);
                }
                else

                    MessageBox.Show("请选择当前状态为在售的进行摇号", "提示");

            }
            else
            {

                MessageBox.Show("请选择一项进行摇号", "提示");
            }


        }

        public override string TableName => "Park";

        protected override bool EachArgForAppend(string n, string value)
        {
            if (n.Equals("note"))
            {
                return true;
            }
            else if (n.Equals("price"))
            {
                double d;
                if (double.TryParse(value, out d))
                {
                    return true;
                }
                return false;
            }
            else
            {
                int d;
                if (int.TryParse(value, out d))
                {
                    if (n.Equals("status") && (d < -1 || d > 1))
                    {
                        return false;
                    }
                    return true;
                }
                else
                    return false;
            }

        }

        public override string AppendStr(ParkInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", 999999.ToString());
        }
        public override string ChangeStr(ParkInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", obj.id);

        }

        public override string DeleteStr(ParkInfo obj)
        {
            return SQLHelpler.MakeSelectStr(TableName, "id", obj.id);
        }
    }
}
